package cn.tju.yanbo.commons;

import cn.tju.yanbo.utils.AppUtils;
import android.content.Intent;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;

public class LayoutViewGestureListener implements OnGestureListener, OnTouchListener {

    protected BaseActivity activity = null;
    private GestureDetector detector = null;
    
    public LayoutViewGestureListener(BaseActivity activity) {
        this.detector = new GestureDetector(this);
        this.activity = activity;
    }
    
    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    /*
     * e1 接触屏幕触点
     * e2 离开屏幕触点
     * velocityX X方向滑动速度
     * velocityY Y方向滑动速度
     * @see android.view.GestureDetector.OnGestureListener#onFling(android.view.MotionEvent, android.view.MotionEvent, float, float)
     */
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        int len4judge = AppUtils.SCREEN_METRICS.widthPixels/4;
        if (e2.getX()-e1.getX()>= len4judge) {
            Intent intent=new Intent(activity, activity.leftActivity());
            activity.startActivity(intent);
            return true;
        } else if (e1.getX() - e2.getX() >= len4judge) {
            Intent intent=new Intent(activity, activity.rightActivity());
            activity.startActivity(intent);
            return true;
        }
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onTouch(View v, MotionEvent ev) {
        return detector.onTouchEvent(ev);
    }

}
